package com.yanqu.road.server.manger.player;

import com.yanqu.road.entity.enums.eCardType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.enums.eNoticeType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.notice.NoticeRecord;
import com.yanqu.road.entity.player.UserCard;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.player.UserSubscribe;
import com.yanqu.road.logic.bussiness.player.UserCardBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.notice.NoticeRecordMgr;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.TimeHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.Date;
import java.util.Map;

public class PrivilegeCardMgr extends TempMgr {

    public static final int PRIVILEGE_CARD_WEEK_DAY = 7;
    public static final int PRIVILEGE_CARD_MONTH_DAY = 30;
    public static final int PRIVILEGE_CARD_YEAR_DAY = 365;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }
    @Override
    public boolean reloadConfig() {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    /**
    * 描述：特权卡是否可以购买，周卡购买判断不走这里
    * 作者：zrq
    * 时间：2023/3/16
    * 参数：

    * 返回值：
    **/
    public static boolean canBuyCard(UserCard userCard,int cardType) {
        if(null == userCard){
            return false;
        }
        if(isInCardTime(userCard, cardType)){//在卡周期内，最后一天才能购买
            int days = 0;
            if(eCardType.MonthCard.getValue() == cardType){
                days = DateHelper.calc2DateTDOADays(new Date(userCard.getMonthCardEndTime() * 1000), new Date())-1;
                return days <= GameConfig.WEEK_CARD_RENEW_REMIND;
            }else if(eCardType.YearCard.getValue() == cardType){
                days = DateHelper.calc2DateTDOADays(new Date(userCard.getYearCardEndTime() * 1000), new Date());
            }
            return days == 1;
        }
        return true;
    }

    public static boolean isInCardTime(UserCard userCard, int cardType) {
        if(null == userCard){
            return false;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        if (cardType == eCardType.MonthCard.getValue() && userCard.getMonthCardEndTime() > nowTime) {
            return true;
        }else if (cardType == eCardType.YearCard.getValue() && userCard.getYearCardEndTime() > nowTime) {
            return true;
        }else if (cardType == eCardType.WeekCard.getValue() && userCard.getWeekCardEndTime() > nowTime) {
            return true;
        }
        return false;
    }

    /**
     * 到期前一天0点后发送邮件通知
     */
    public static void checkSendExpireMail() {
        long nowTime = System.currentTimeMillis(); // 毫秒
        long todayZeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000; // 秒
        long tomorrowZeroTime = todayZeroTime + DateHelper.DAY_SECONDS; // 秒
        long afterTomZeroTime = tomorrowZeroTime + DateHelper.DAY_SECONDS; // 秒
        Map<Long, UserCard> userCardMap = UserCardBussiness.getUserCardMapByTime(tomorrowZeroTime, afterTomZeroTime - 1);
        for (UserCard userCard : userCardMap.values()) {
            // 判断月卡
            if (userCard.getMonthCardEndTime() >= tomorrowZeroTime && userCard.getMonthCardEndTime() < afterTomZeroTime) {
                NoticeRecord monthRecord = NoticeRecordMgr.getNoticeRecord(userCard.getUserId(), eNoticeType.MONTH_CARD_EXPIRE);
                if (!(monthRecord != null && monthRecord.getNotifyTime() / 1000 >= todayZeroTime)) {
                    String language = UserMgr.getLanguage(userCard.getUserId());
                    sendExpireMail(userCard, eCardType.MonthCard, language);
                    if (monthRecord == null) {
                        NoticeRecordMgr.addNoticeRecord(userCard.getUserId(), eNoticeType.MONTH_CARD_EXPIRE, nowTime);
                    } else {
                        monthRecord.setNotifyTime(nowTime);
                    }
                }
            }
            // 判断年卡
            if (userCard.getYearCardEndTime() >= tomorrowZeroTime && userCard.getYearCardEndTime() < afterTomZeroTime) {
                NoticeRecord yearRecord = NoticeRecordMgr.getNoticeRecord(userCard.getUserId(), eNoticeType.YEAR_CARD_EXPIRE);
                if (!(yearRecord != null && yearRecord.getNotifyTime() / 1000 >= todayZeroTime)) {
                    String language = UserMgr.getLanguage(userCard.getUserId());
                    sendExpireMail(userCard, eCardType.YearCard, language);
                    if (yearRecord == null) {
                        NoticeRecordMgr.addNoticeRecord(userCard.getUserId(), eNoticeType.YEAR_CARD_EXPIRE, nowTime);
                    } else {
                        yearRecord.setNotifyTime(nowTime);
                    }
                }
            }
        }
    }

    private static void sendExpireMail(UserCard userCard, eCardType sendType, String language) {
        String title = null;
        String content = null;
        String timeString = null;
        if (eCardType.MonthCard.equals(sendType)) {
            timeString = TimeHelper.getDateFormat(new Date(userCard.getMonthCardEndTime() * 1000), "yyyy-MM-dd HH:mm:ss");
            long userId = userCard.getUserId();
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
            boolean isLock = false;
            if (player == null) {
                UserInfo userInfo = UserMgr.getUserInfo(userId);
                if (userInfo != null) {
                    isLock = SystemOpenMgr.offlineSystemOpen(userInfo.getSystemOpenValue(), eSystemId.Assistant.getValue());
                }
            }else {
                isLock = SystemOpenMgr.systemOpen(player, eSystemId.Assistant.getValue());
            }
            if (isLock) {
                title = MultipleLanguageMgr.getContent(MailManager.MONTH_CARD_EXPIRE_TITLE_ADD, language);
                content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.MONTH_CARD_EXPIRE_CONTENT_ADD, language), timeString);
            }else {
                title = MultipleLanguageMgr.getContent(MailManager.MONTH_CARD_EXPIRE_TITLE, language);
                content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.MONTH_CARD_EXPIRE_CONTENT, language), timeString);
            }
        } else if (eCardType.YearCard.equals(sendType)) {
            timeString = TimeHelper.getDateFormat(new Date(userCard.getYearCardEndTime() * 1000), "yyyy-MM-dd HH:mm:ss");
            title = MultipleLanguageMgr.getContent(MailManager.YEAR_CARD_EXPIRE_TITLE, language);
            content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.YEAR_CARD_EXPIRE_CONTENT, language), timeString);
        } else {
            return;
        }
        MailManager.sendMail(userCard.getUserId(), eMailType.SYSTEM.getValue(), "", content, title);
    }

    /**
     * 是否领取特权卡奖励
     * @param userCard
     * @param cardType
     * @return
     */
    public static int isGetCardReward(UserCard userCard, int cardType) {
        if(null == userCard){
            return 0;
        }
        if(!isInCardTime(userCard, cardType)){
            return 0;
        }
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000;
        if (cardType == eCardType.MonthCard.getValue()) {
            if(userCard.getGetMonthCardRewardTime() >= zeroTime){
                return 1;
            }else {
                return 0;
            }
        }else if (cardType == eCardType.YearCard.getValue()) {
            if(userCard.getGetYearCardRewardTime() >= zeroTime){
                return 1;
            }else {
                return 0;
            }
        }else {
            return 0;
        }
    }

    /**
    * 描述：周卡是否可以购买
    * 作者：zrq
    * 时间：2023/3/16
    * 参数：

    * 返回值：
    **/
    public static boolean canBuyWeekCard(Map<Integer, UserSubscribe> subscribeMap, int mallId) {
        UserSubscribe userSubscribe = subscribeMap.get(GameConfig.SUBSCRIBE_WEEK_CARD_MALL_ID);
        if(GameConfig.PRIVILEGE_WEEK_CARD_MALL_ID == mallId || GameConfig.SUBSCRIBE_WEEK_CARD_MALL_ID == mallId){
            if(null == userSubscribe){//没有订阅，可以购买
                return true;
            }
            if(userSubscribe.getType() == 0 || userSubscribe.getType() == 1){
                long nowTime = System.currentTimeMillis() / 1000;
                if(nowTime < userSubscribe.getExpiresTime()){//订阅中，不可以购买
                    return false;
                }
            }
            return true;
        }else {
            return false;
        }

    }
}
